package surreal.triviamaster.gui;

import surreal.triviamaster.R;
import surreal.triviamaster.utilities.TriviaMasterApplication;
import surreal.triviamaster.utilities.Utilities;
import android.app.ListActivity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class AddHints extends ListActivity {

	private Bundle qBundle;
	private String fList = "";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		setContentView(R.layout.add_hints);

		// getting the data that was passed to this activity
		Bundle bundle = getIntent().getExtras();
		qBundle = bundle.getBundle("bundle");

		//setting the GUI
		((TextView) findViewById(R.id.hint)).setText(qBundle.getString("hint"));
		fList= qBundle.getString("fList");

		setListActivity();

		ListView lv = getListView();
		lv.setTextFilterEnabled(true);
		
		//Add False Answer
	    Button addAnswer = (Button) findViewById(R.id.addAnswer);
	    addAnswer.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	String a=((TextView) findViewById(R.id.fAnswer)).getText().toString();
            	if(a.equals("")||Utilities.isValidToUpload(a)==false) {
            		Toast.makeText(AddHints.this, TriviaMasterApplication.ERROR_INVALID_CHARACTERS, 10000).show();
            		
            	} else if(fList.equals("")) {
            		//if the aAs is empty just add the answer
            		fList=a;
            		((TextView) findViewById(R.id.fAnswer)).setText("");
            	} else {
            		//else add a delimiter before the answer
            		fList+=";"+a;
            		((TextView) findViewById(R.id.fAnswer)).setText("");
            	}
            	setListActivity();
            }
        });

		// Next Page
		Button nextPage = (Button) findViewById(R.id.nextPage);
		nextPage.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				String hint=((TextView) findViewById(R.id.hint)).getText().toString();
            	if(fList.equals("")&&hint.equals("")){
            		//if user has not supplied any answers
            		Toast.makeText(AddHints.this, "You must add one hint or at least one false answer", 10000).show();
            		
            	} else if (Utilities.isValidToUpload(hint)==false){
            		Toast.makeText(AddHints.this, TriviaMasterApplication.ERROR_INVALID_CHARACTERS, 10000).show();
            	}else {
            		
            		//passing question data to the next page
            		qBundle.putString("fList", fList);
            		qBundle.putString("hint", hint);
            		
            		Intent intent = new Intent(AddHints.this, AddCategoriesToQuestion.class);
            		intent.putExtra("bundle", qBundle);
	  	      		startActivity(intent);
            	}
			}
		});

		// Cancel button
		Button cancel = (Button) findViewById(R.id.cancel);
		cancel.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {

				// Move user to next page
				Intent intent = new Intent(AddHints.this,
						AddQuestion.class);
				intent.putExtra("bundle", qBundle);
				startActivity(intent);
			}
		});
	}
	
	private void setListActivity() {
		
    	String[] temp=fList.split(";");
    	setListAdapter(new ArrayAdapter<String>(this, R.layout.generic_list_item, temp));
		
	}
}
